package com.dyzs.activity.word;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;

import com.dyzs.activity.CommonExpandableListActivity;
import com.dyzs.activity.R;
import com.dyzs.activity.tool.game.MyExpandableListAdapter;
import com.dyzs.domain.City;

public class ProviceListActivity extends CommonExpandableListActivity {
	
	private	List<String> group;
	private List<List<Object>> child;	
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.provice_list);	
		group=Arrays.asList(getIntent().getStringArrayExtra("proviceList"));
		child=getAllCityList();
		MyExpandableListAdapter mAdapter = new MyExpandableListAdapter(this,child,group);       
		this.setListAdapter(mAdapter);		
		this.getExpandableListView().setOnChildClickListener(new ExpandableListView.OnChildClickListener(){
			@Override
			public boolean onChildClick(ExpandableListView arg0, View arg1,
					int arg2, int arg3, long arg4) {
				 // get parent data
				ExpandableListAdapter adapter = arg0.getExpandableListAdapter();
				 // get child data
				City t=(City)adapter.getChild(arg2, arg3);				
				Intent intent = new Intent(ProviceListActivity.this,WordListActivity.class);;	
				intent.putExtra("cityId", t.getCityCode());
				startActivity(intent);
				return false;
			}
		});
	}

	

	public List<List<Object>>getAllCityList(){		
		String[] proviceStr=getResources().getStringArray(R.array.provice_names);		
		String[] cityStr=getResources().getStringArray(R.array.city_names);	
		List<List<Object>> cityList=new ArrayList<List<Object>>();
		for(String provice:group){
			List<Object> list=new ArrayList<Object>();	
			String proviceCode="";
			for(int k=0;k<proviceStr.length;k++){
				if(proviceStr[k].startsWith(provice)){
					proviceCode=proviceStr[k].split(",")[1].substring(0,2);
					break;
				}
			}
					
			for(int j=0;j<cityStr.length;j++){
				if(cityStr[j].startsWith(proviceCode)){
					City city=new City();
					city.setCityCode(cityStr[j].split(",")[0]);
					city.setCityName(cityStr[j].split(",")[1]);
					list.add(city);
				}
			}
			cityList.add(list);
		}		
		return cityList;
	}
	
	
	

	public void onClick(View v){
		if(v.getId()==R.id.word_main_bt){
			Intent it = new Intent(this, WordCollectionActivity.class);
			it.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			this.startActivity(it);
		}else if(v.getId()==R.id.word_main_esc){			
			Intent it = new Intent(this,WordListActivity.class);
			it.putExtra("searchTitle", ((EditText)this.findViewById(R.id.word_main_search)).getText().toString());
			it.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			this.startActivity(it);
		}
		
	}

}
